package org.jeecg.modules.compilation.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.compilation.dto.AddNoteDTO;
import org.jeecg.modules.compilation.dto.NoteDTO;
import org.jeecg.modules.compilation.dto.NoteFileDTO;
import org.jeecg.modules.compilation.entity.CiNote;
import org.jeecg.modules.compilation.dto.NotesDTO;
import org.jeecg.modules.compilation.service.ICiNoteService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.compilation.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: ci_note
 * @Author: jeecg-boot
 * @Date:   2024-03-15
 * @Version: V1.0
 */
@Api(tags="ci_note")
@RestController
@RequestMapping("/compilation/ciNote")
@Slf4j
public class CiNoteController extends JeecgController<CiNote, ICiNoteService> {
	@Autowired
	private ICiNoteService ciNoteService;

	// --------------------- 自定义接口 --------------------------------
	@ApiOperation(value="笔记的首页列表",notes = "笔记列表")
	@GetMapping(value = "/notes")
	public Result<List<NotesDTO>> getNotes() {
		//获取登录用户信息
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userId = loginUser.getId();

		List notesList = ciNoteService.getNoteCount(userId);
		return Result.OK(notesList);
	}

	/*
	同一文件下笔记列表
	 */
	 @ApiOperation(value="笔记列表-同一文件",notes = "笔记列表-同一文件")
	 @GetMapping(value = "/notefile")
	 public Result<List<NoteFileDTO>> getnoteFile(@RequestParam("documentSn") String documentSn) {
//		//获取登录用户信息
//		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//		String userId = loginUser.getId();
//
		//通过用户id和文件编号来获取文件名和创建时间
		List noteFile = ciNoteService.getnote(documentSn);
		 return Result.OK(noteFile);
	 }

	 /*
	 有id传入则为编辑，无id传入则为新增
	  */
	@AutoLog(value = "新增和编辑笔记")
	@ApiOperation(value = "新增和编辑笔记",notes = "新增和编辑笔记")
	@PostMapping("/addOrUpdate")
	public Result<String> addOrUpdate(@RequestBody CiNote ciNote) {
		boolean x = (ciNote.getDocumentSn() == "");
		if( ciNoteService.addOrUpdate(ciNote) )
			return Result.OK("成功");
		else
			return Result.error("失败");
	}

	/**
	 * 笔记查询
	 *
	 * @param noteName
	 * @return
	 */
	@AutoLog(value = "笔记查询")
	@ApiOperation(value = "笔记查询",notes = "笔记查询")
	@PostMapping("/searchNote")
	public Result<List<NoteDTO>> addOrUpdate(@RequestBody String noteName) throws IOException {
			return Result.OK(ciNoteService.searchNoteList(noteName));
	}

	// --------------------- 自定义接口 --------------------------------
	/**
	 * 分页列表查询
	 *
	 * @param ciNote
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "ci_note-分页列表查询")
	@ApiOperation(value="ci_note-分页列表查询", notes="ci_note-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CiNote>> queryPageList(CiNote ciNote,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CiNote> queryWrapper = QueryGenerator.initQueryWrapper(ciNote, req.getParameterMap());
		Page<CiNote> page = new Page<CiNote>(pageNo, pageSize);
		IPage<CiNote> pageList = ciNoteService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param ciNote
	 * @return
	 */
	@AutoLog(value = "ci_note-添加")
	@ApiOperation(value="ci_note-添加", notes="ci_note-添加")
//	@RequiresPermissions("compilation:ci_note:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CiNote ciNote) {
		ciNote.setNoteSn(CommonUtil.getSn());
		ciNoteService.save(ciNote);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param ciNote
	 * @return
	 */
	@AutoLog(value = "ci_note-编辑")
	@ApiOperation(value="ci_note-编辑", notes="ci_note-编辑")
//	@RequiresPermissions("compilation:ci_note:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CiNote ciNote) {
		ciNoteService.updateById(ciNote);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @return
	 */
	@AutoLog(value = "ci_note-通过id删除")
	@ApiOperation(value="ci_note-通过id删除", notes="ci_note-通过id删除")
//	@RequiresPermissions("compilation:ci_note:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="noteSn",required=true) String noteSn) {
		LambdaQueryWrapper<CiNote> ciNoteLambdaQueryWrapper = new LambdaQueryWrapper<>();
		ciNoteLambdaQueryWrapper.eq(CiNote::getNoteSn,noteSn);
		ciNoteService.remove(ciNoteLambdaQueryWrapper);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ci_note-批量删除")
	@ApiOperation(value="ci_note-批量删除", notes="ci_note-批量删除")
//	@RequiresPermissions("compilation:ci_note:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.ciNoteService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "ci_note-通过id查询")
	@ApiOperation(value="ci_note-通过id查询", notes="ci_note-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CiNote> queryById(@RequestParam(name="id",required=true) String id) {
		CiNote ciNote = ciNoteService.getById(id);
		if(ciNote==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ciNote);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ciNote
    */
//    @RequiresPermissions("compilation:ci_note:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CiNote ciNote) {
        return super.exportXls(request, ciNote, CiNote.class, "ci_note");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("compilation:ci_note:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CiNote.class);
    }

}
